package com.kye.express.business.personal;

import android.arch.lifecycle.Lifecycle;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.IView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.RequestUser;
import com.kye.model.bean.user.User;
import com.kye.model.service.BillService;

import java.util.Map;

import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 18/12/2017
 * @updated : 18/12/2017
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public interface PersonalContract {

    interface PersonalView extends IView {

        void onUser(User user);

        void onCommentCount(int commentCount);

        void onBalance(float balance);

        void onCredits(int credits);

        void onNewBillsVisibility(boolean newBillsVisibility);
    }

    class PersonalPresenter extends BasePresenter<PersonalView> {

        @Override
        public void bind(PersonalView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().getProvider().<User>bindUntilEvent(Lifecycle.Event.ON_DESTROY))
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onUser(user);
                        }
                    });
        }

        public void hasNewBill() {
            UserSession.getInstance()
                    .getUserForRx()
                    .firstOrError()
                    .flatMap(new Function<User, Single<OpenApiResult<Map<String, Integer>>>>() {
                        @Override
                        public Single<OpenApiResult<Map<String, Integer>>> apply(User user) throws Exception {
                            return RetrofitManager.getInstance()
                                    .createOpenApiService(BillService.class)
                                    .hasNewBill(new RequestUser(user.getPhone()));
                        }
                    })
                    .compose(getView().bindToLifecycle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<Map<String, Integer>>() {
                        @Override
                        public void onLoading(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<Map<String, Integer>> result, Map<String, Integer> stringBooleanMap) {
                            for (Integer integer : stringBooleanMap.values()) {
                                if (1 == integer) {
                                    getView().onNewBillsVisibility(true);
                                    return;
                                }
                            }

                            getView().onNewBillsVisibility(false);
                        }

                        @Override
                        public void onApiError(OpenApiResult<Map<String, Integer>> result, int code, String msg) {

                        }

                        @Override
                        public void onOtherError(Throwable e) {

                        }
                    });
        }
    }
}
